package cn.com.common.tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {
	/**
	 * set session value
	 * 
	 * @param request
	 * @param skey
	 * @param svalue
	 * @param maxtime
	 *            session max time
	 * @return session set state
	 */
	public static String setSession(HttpServletRequest request, String skey,
			String svalue, Integer maxtime) {
		HttpSession session = request.getSession();
		if (skey == null || "".equals(skey)) {
			return "false";
		}
		session.setAttribute(skey, svalue);
		if (maxtime > 0) {
			session.setMaxInactiveInterval(maxtime);
		}
		return "ok";
	}

	/**
	 * get session value
	 * 
	 * @param request
	 * @param skey
	 *            session key
	 * @return session value
	 */
	public static String getSession(HttpServletRequest request, String skey) {
		if (skey == null || "".equals(skey)) {
			return "";
		}
		HttpSession session = request.getSession();
		Object objsession = session.getAttribute(skey);
		if (objsession != null) {
			return objsession.toString();
		}
		return "";
	}

	/**
	 * delete session
	 * 
	 * @param request
	 * @param skey
	 *            session key if key is "" or null delete all
	 * @return delete state
	 */
	public static String delSession(HttpServletRequest request, String skey) {
		HttpSession session = request.getSession();
		if (skey == null || "".equals(skey)) {
			session.invalidate();
			return "ok";
		}
		session.removeAttribute(skey);
		return "ok";
	}
}
